/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package dk.redillusions.jdbc.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Invocation handler for use when creating proxy {@link PreparedStatement}s.
 * 
 * @author Sune Wettersteen
 * 
 */
final class PreparedStatementInvocationHandler implements InvocationHandler {
    /**
     * The actual jdbc statement.
     */
    private Statement actualPreparedStatement;

    /**
     * The list of statement proxies.
     */
    private List<PreparedStatement> preparedStatements = new ArrayList<PreparedStatement>();

    /**
     * Constructor.
     * 
     * @param preparedStatement
     *            The actual {@link PreparedStatement}
     */
    public PreparedStatementInvocationHandler(final PreparedStatement preparedStatement) {
        this.actualPreparedStatement = preparedStatement;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
        /*
         * Run through all the prepared statement implementations and invoke the
         * method
         */
        for (PreparedStatement preparedStatement : this.preparedStatements) {
            /*
             * We do not handle exceptions thrown from this. It is not
             * acceptable that any are thrown
             */
            method.invoke(preparedStatement, args);
        }

        // Invoke the method on the actual prepared statement
        try {
            return method.invoke(this.actualPreparedStatement, args);
        } catch (InvocationTargetException e) {
            final Throwable cause = e.getCause();

            // We only accept SQLException to be thrown
            if (cause instanceof SQLException) {
                throw cause;
            }

            throw e;
        }
    }

	/**
	 * Add a {@link PreparedStatement} to the invocation handler. All methods
	 * invoked on any proxy for this invocation handler will be delegated to the
	 * {@link PreparedStatement}s added to the invocation handler.
	 * 
	 * @param preparedStatement
	 *            The prepared statement delegate to add
	 */
    public void addPreparedStatementDelegate(final PreparedStatement preparedStatement) {
        this.preparedStatements.add(preparedStatement);
    }
}
